package com.itheima.controller.results;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.itheima.system.exception.BizException;

import java.io.Serializable;

/**
 * Author:      chenliuchun
 * Date:        2018/6/5
 * Description: 通用接口返回参数类
 * Modification History:
 * Date       Author       Version     Description
 * -----------------------------------------------------
 *
 * @author zccp
 */

//序列化json的时候,如果是null的对象, key也会消失
public class Result<T> implements Serializable {

    private String code;
    private String message;
    private T detail;

    public Result() {
    }

    public Result(String code) {
        this.code = code;
    }

    private Result(String code, T detail) {
        this.code = code;
        this.detail = detail;
    }

    private Result(String code, String message) {
        this.code = code;
        this.message = message;
    }

    private Result(String code, String message, T detail) {
        this.code = code;
        this.message = message;
        this.detail = detail;
    }

    @JsonIgnore //使之不在json序列化结果当中
    public boolean isSuccess() {
        return ErrorCode.SUCCESS.name().equals(this.code);
    }

    public String getCode() {
        return code;
    }

    public T getDetail() {
        return detail;
    }

    public String getMessage() {
        return message;
    }


    public static <T> Result<T> ofSuccess() {
        return new Result<T>(ErrorCode.SUCCESS.name());
    }

    public static <T> Result<T> ofSuccessMsg(String msg) {
        return new Result<T>(ErrorCode.SUCCESS.name(), msg);
    }

    public static <T> Result<T> ofSuccess(T data) {
        return new Result<T>(ErrorCode.SUCCESS.name(), data);
    }

    public static <T> Result<T> ofSuccess(String msg, T data) {
        return new Result<T>(ErrorCode.SUCCESS.name(), msg, data);
    }

    public static <T> Result<T> ofError() {
        return new Result<T>(ErrorCode.SYSTEM_ERROR.name(), ErrorCode.SYSTEM_ERROR.getMessage());
    }

    public static <T> Result<T> ofError(String errorMessage) {
        return new Result<T>(ErrorCode.SYSTEM_ERROR.name(), errorMessage);
    }

    public static <T> Result<T> ofError(String errorCode, String errorMessage) {
        return new Result<T>(errorCode, errorMessage);
    }

    public static <T> Result<T> ofError(ErrorCode errorCode) {
        return new Result<T>(errorCode.name(), errorCode.getMessage());
    }

    public static <T> Result<T> ofError(ErrorCode errorCode, String errorMessage) {
        return new Result<T>(errorCode.name(), errorMessage);
    }

    public static <T> Result<T> ofError(BizException e) {
        return new Result<T>(e.getCode(), e.getMessage());
    }

    @Override
    public String toString() {
        return "Result{" +
                "code='" + code + '\'' +
                ", msg=" + message +
                ", detail=" + detail +
                '}';
    }

}




